package com.dms.modules.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dms.common.exception.BusinessException;
import com.dms.modules.product.entity.Product;
import com.dms.modules.product.entity.ProductFavorite;
import com.dms.modules.product.mapper.ProductFavoriteMapper;
import com.dms.modules.product.service.ProductFavoriteService;
import com.dms.modules.product.service.ProductService;
import com.dms.modules.product.vo.ProductFavoriteVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品收藏服务实现类
 */
@Service
public class ProductFavoriteServiceImpl extends ServiceImpl<ProductFavoriteMapper, ProductFavorite> implements ProductFavoriteService {

    @Autowired
    private ProductService productService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addFavorite(Long userId, Long productId) {
        // 检查商品是否存在
        Product product = productService.getById(productId);
        if (product == null) {
            throw new BusinessException("商品不存在");
        }
        
        // 检查是否已收藏
        if (isFavorite(userId, productId)) {
            throw new BusinessException("商品已收藏");
        }
        
        // 添加收藏
        ProductFavorite favorite = new ProductFavorite();
        favorite.setUserId(userId);
        favorite.setProductId(productId);
        return save(favorite);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean cancelFavorite(Long userId, Long productId) {
        LambdaQueryWrapper<ProductFavorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductFavorite::getUserId, userId)
                .eq(ProductFavorite::getProductId, productId);
        return remove(wrapper);
    }

    @Override
    public IPage<ProductFavoriteVO> getUserFavorites(Long userId, Integer pageNum, Integer pageSize) {
        Page<ProductFavorite> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ProductFavorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductFavorite::getUserId, userId)
                .orderByDesc(ProductFavorite::getCreateTime);
        
        IPage<ProductFavorite> favoritePage = page(page, wrapper);
        return favoritePage.convert(favorite -> {
            ProductFavoriteVO vo = new ProductFavoriteVO();
            BeanUtils.copyProperties(favorite, vo);
            
            // 设置商品信息
            Product product = productService.getById(favorite.getProductId());
            if (product != null) {
                vo.setProductName(product.getName());
                vo.setProductImage(product.getMainImage());
                vo.setPrice(product.getPrice());
                vo.setStatus(product.getStatus());
            }
            
            return vo;
        });
    }

    @Override
    public boolean isFavorite(Long userId, Long productId) {
        LambdaQueryWrapper<ProductFavorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductFavorite::getUserId, userId)
                .eq(ProductFavorite::getProductId, productId);
        return count(wrapper) > 0;
    }

    @Override
    public Integer getFavoriteCount(Long productId) {
        return Math.toIntExact(lambdaQuery()
                .eq(ProductFavorite::getProductId, productId)
                .eq(ProductFavorite::getDeleted, 0)
                .count());
    }

    @Override
    public IPage<ProductFavorite> getFavoriteList(Long userId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<ProductFavorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductFavorite::getUserId, userId)
               .orderByDesc(ProductFavorite::getCreateTime);
        
        Page<ProductFavorite> page = new Page<>(pageNum, pageSize);
        return page(page, wrapper);
    }
} 